package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetailService orderDetailService;




    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R page(Integer page,Integer pageSize){
        IPage<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getCheckoutTime);
        IPage<Orders> iPage = orderService.page(pageInfo, lqw);
        IPage<OrdersDto> ordersDtoIPage = new Page<>();
        BeanUtils.copyProperties(iPage,ordersDtoIPage,"records");
        List<Orders> records = iPage.getRecords();
        List<OrdersDto> list = records.stream().map(record -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            Long recordId = record.getId();
            LambdaQueryWrapper<OrderDetail> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(OrderDetail::getOrderId,recordId);
            List<OrderDetail> orderDetailList = orderDetailService.list(lqw2);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoIPage.setRecords(list);
        return R.success(ordersDtoIPage);
    }


    @GetMapping("/page")
    public R pageWeb(Integer page, Integer pageSize, String number,String beginTime,String endTime){
        IPage<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(number)){
            lqw.like(Orders::getNumber,number);
        }
        if (StringUtils.isNotEmpty(beginTime)&&StringUtils.isNotEmpty(endTime)){
            lqw.between(Orders::getOrderTime,beginTime,endTime);
        }
        IPage<Orders> iPage = orderService.page(pageInfo, lqw);
        return R.success(iPage);
    }

    @PutMapping
    public R update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("修改成功");
    }
}

